/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Jhovany Amaya
 */
public class FormReportValidator {

    public FormReportValidator() {
    }
    /**
     * 
     * @param amount
     * @return 
     */
    public static double validateAmount (final String amount){
        double parsedAmount;
        try{
            parsedAmount = Double.parseDouble(amount);
        }catch(Exception e){
            return -1;
        }
        return parsedAmount;
    }
    
    /**
     * 
     * @param date Formato esperado de entrada dd/mm/aaaa
     * @return True si la fecha es correcta sintacticamente
     */
    public static boolean syntacticValidationDate(final String date){
        if(date.length()!="dd/mm/aaaa".length())
            return false;
        Pattern pattern=Pattern.compile("(?:[0-9]{4}/[0-9]{2}{2})");
        Matcher match=pattern.matcher(date);
        if(!match.find())
            return false;
        int year=Integer.parseInt(date.substring(0,4));
        int month=Integer.parseInt(date.substring(5, 7));
        int day=Integer.parseInt(date.substring(8));
        boolean leapYear=year%4==0;
        boolean vadidDate=true;
        if(day>31 || month>12)
              vadidDate=false;
        else if(!leapYear && month==2 && day==29)
              vadidDate=false;
        else if(month==2 && day>29)
              vadidDate=false;
        else if(day==31 && (month==4 || month==6 || month==9 || month==1))
              vadidDate=false;
        return vadidDate;
    }
    
    /**
     * 
     * @param date String con fecha en formato aaaa/mm/dd
     * @return Representacion de la fecha en objeto Date. Es null si 
     * es una fecha invalida
     */
    public static Date semanticValidationDate(final String date){
        DateFormat formatter=new SimpleDateFormat("yyyy/MM/dd");
        Date dateD;
        try {
            dateD=(Date)formatter.parse(date);
        } catch (ParseException ex) {
            dateD=null;
        }
        return dateD;
    }
}
